<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\api\logic;



use app\api\model\UserModel;
use think\Model;
use my\Security;
class UserLogic extends Model
{


	public function __construct()
	{
		parent::__construct();

	}
	/**
	 * 乘客端的余额设置和行为记录
     * $order_sn 订单号
     * $common_id 订单ID
     * $uid 用户ID
     * $type 付款类型空是余额1支付宝2微信3余额4余额+支付宝5余额+微信
     * $third_pay 第三方付款金额
     * $wallet_pay 余额付款金额
	 */
	public function setUserMoney($order_sn,$common_id,$uid,$type,$third_pay,$wallet_pay)
	{
        $userModel=new UserModel();
        return $userModel->setUserMoney($order_sn,$common_id,$uid,$type,$third_pay,$wallet_pay);
	}

	/**
	 * 乘客端设置密码和修改密码忘记密码
	 *
	 **/
	public function setPass($user_phone,$user_pass){
		$user_phone = Security::encrypt($user_phone);
		$user_pass = md5($user_pass);
		$userModel=new UserModel();
		return $userModel->setPass($user_phone, $user_pass);
	}
	/**
	 * 获取会员信息
	 * @param $uid
	 * @param $string //搜索条件
	 * @return array
	 */
	public function getUserByUid($uid, $string)
	{
		$userModel=new UserModel();
		return $userModel->getUserByUid($uid, $string);
	}
	/**
	 * 判断下单时间是否已经超过15分钟
	 * @param $uid
	 * @param $string //搜索条件
	 * @return array
	 */
	public function set_order_cancel($uid)
	{
		$userModel=new UserModel();
		return $userModel->set_order_cancel($uid);
	}

	/**
	 * 根据电话获取会员信息
	 * @param $r_phone
	 * @param $string
	 * @return array
	 */
	public function getUserByPhone($phone, $string)
	{
		$userModel=new UserModel();
		return $userModel->getUserByPhone($phone, $string);
	}
	/**
	 * 添加用户登录日志
	 */
	public function createUserLog($data){
		$userModel=new UserModel();
		return $userModel->createUserLog($data);
	}
	/**
	 * 新建会员
	 * @param $r_phone
	 * @return mixed
	 */
	public function createUser($data)
	{
		//unset($data['user_repass']);

		$userModel=new UserModel();
		return $userModel->createUser($data);
	}

	/**
	 * 计算失败次数
	 * @param $user_info
	 * @return mixed
	 */
	public function freezeUser($user_info)
	{
		$data['uid']=$user_info["uid"];
		$data['error_time']=time();
		if($data['error_time']-$user_info['error_time']>300){
			$user_info['wrong_count']=0;
		}
		$data['wrong_count']=$user_info['wrong_count']+1;
		if($data['wrong_count']>=5){
			$data['can_time']=time()+3600;
		}
		$userModel=new UserModel();
		return $userModel->UpdateUser($data);
	}

	/**
	 * 会员登陆成功
	 * @param $user_info
	 * @return string
	 */
	public function userLogin($user_info)
	{
		$data['uid']=$user_info['uid'];
		$data['last_login_time']=time();
		$data['token']=md5(date("YmdHis",time()).rand(10000,99999));
		$userModel=new UserModel();
		$userModel->UpdateUser($data);
		return $data['token'];
	}

	/**
	 * 忘记密码
	 * @param $pass
	 * @param $b_phone
	 */
	public function forgerPass($pass, $uid)
	{
		$data['user_pass']=md5($pass);
		$data['uid']=$uid;
		$data['wrong_count']=0;
		$data['can_time']=0;
		$userModel=new UserModel();
		$userModel->UpdateUser($data);
	}

	/**
	 * @param $uid
	 * @return array
	 */
	public function sign($uid)
	{
		$userModel=new UserModel();
		$sign=$userModel->getSign($uid);
		if(!empty($sign)){
			return array("status"=>0,"msg"=>"今天已签到!");
		}
		$r=$userModel->sign($uid);
		return array("status"=>1,"data"=>$r);
	}


}